package com.zxl.gtion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxl.gtion.entity.Prize;
import com.zxl.gtion.entity.Subject;
import com.zxl.gtion.entity.Topic;
import com.zxl.gtion.pojo.BaiduErnie4Message;
import com.zxl.gtion.service.SubjectService;
import com.zxl.gtion.service.TopicService;
import com.zxl.gtion.util.R;
import com.zxl.gtion.util.ShiroUtils;
import com.zxl.gtion.vo.SubjectVO;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * (Subject)表控制层
 *
 * @author
 * @since 2024-03-06 16:45:01
 */
@RestController
@RequestMapping("subject")
@RequiredArgsConstructor
public class SubjectController {
    /**
     * 服务对象
     */
    private final SubjectService subjectService;
    private final TopicService topicService;

    @GetMapping("answerInterface")
    @ApiOperation("答题界面")
    public R<List<SubjectVO>> answerInterface() {
        return R.ok(this.subjectService.answerInterface());
    }

    @PostMapping("userAnswer")
    @ApiOperation("用户答题")
    public R<Prize> userAnswer(@RequestBody List<SubjectVO> subjectVOS) {
        return R.ok(this.subjectService.userAnswer(subjectVOS));
    }

    @GetMapping("myPrize")
    @ApiOperation("我的奖励")
    public R<List<Prize>> myPrize() {
        return R.ok(this.subjectService.myPrize());
    }

    @GetMapping("isHavePrize")
    @ApiOperation("是否拥有勋章")
    public R<Boolean> isHavePrize() {
        return R.ok(this.subjectService.isHavePrize());
    }

    @PostMapping("feeBack")
    @ApiOperation("反馈")
    public R feeBack(@RequestBody Topic topic){

//        topic.setUserId(ShiroUtils.getUserId());
        topic.setUserId(1);
        return R.ok(topicService.save(topic));
    }


    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param subject 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Subject> page, Subject subject) {
        return R.ok(this.subjectService.page(page, new QueryWrapper<>(subject)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.subjectService.getById(id));
    }

    private final StringRedisTemplate redisTemplate;

    /**
     * 新增数据
     *
     * @param subject 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Subject subject) {

        return R.ok(this.subjectService.save(subject));
    }

    /**
     * 修改数据
     *
     * @param subject 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Subject subject) {
        return R.ok(this.subjectService.updateById(subject));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.subjectService.removeByIds(idList));
    }
}

